<?php

namespace Modules\Admin\Http\Controllers;

use HiCommon\Repository\AdminsRepository;
use HiCommon\Repository\ShopUserRepository;
use HiCommon\Repository\UsersRepository;
use HiCommon\Service\Clear;
use HiCommon\Service\FinanceAdminService;
use HiCommon\Service\MenuService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Request;
use Modules\Admin\Service\AdminsService;
use Modules\Admin\Service\LogService;

class IndexController extends BaseController
{
    public function main()
    {
        //获取菜单
        $arr_admin = admin_user()->toarray();
        $n_uid = $arr_admin['id'];
        $arr_menu = MenuService::api_menu_admin($n_uid);
        $arr_data = [
          'arr_admin'=>$arr_admin,
          'arr_menu_list'=>$arr_menu,
        ];
        return view('admin::index.main',$arr_data);
    }

    public function welcome()
    {
        $result = FinanceAdminService::api_total_analysis();

        $arr_data = $result['data'];
        return view('admin::index.welcome',$arr_data);
    }

    public function finance_graphs()
    {
        //        $s_date_type = Request::input('date_type','');
        $s_date_type = 'half_year';
        $result = FinanceAdminService::api_nearly_order_graphs($s_date_type);
        if($result['state'] != 0){
            msg_error($result['msg']);
            return 'ok';
        }

        $arr_data = $result['data'];
        $data = ['arr_data'=>json_encode($arr_data),'s_date_type'=>$s_date_type];

        return view('admin::index.finance_graphs',$data);
    }
    public function unicode()
    {
        return view('admin::public.unicode');
    }

    /**
     * 后台登录页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function login()
    {
        return view('admin::index.login');
    }

    /**
     * 后台用户登录操作
     *
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function loginIn()
    {
        $arr_input = Request::input();//登录表单信息
        $arr_input['ip'] = (string)Request::ip();//登录ip
        return AdminsService::login_in($arr_input);
    }

    /**
     * 后台用户退出登录操作
     *
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function loginOut(){
        return AdminsService::login_out();
    }

    /**
     * 清除缓存
     *
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function clearCache()
    {
        return view('admin::index.clear_cache');

    }

    /**
     * 清除缓存操作
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function clearCacheOperation()
    {
        $arr_input = Request::input();
        return Clear::clear_cache_operate($arr_input);
    }

    /**
     * 操作日志列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function actionLog()
    {
        $res = LogService::get_admin_action_log_list();
        echo_error($res);
        return view('admin::index.action_log', $res['data']);
    }

    /**
     * 登录日志
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function loginLog()
    {
        $res = LogService::get_admin_login_log();
        echo_error($res);
        return view('admin::index.login_log', $res['data']);
    }

    /**
     * 验证码日志
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function SmsLog()
    {
        $arr_search = Request::input();
        $res = LogService::get_phone_code_log( (array) $arr_search );
        echo_error($res);
        return view('admin::index.sms_log', $res['data'] );
    }

    /**
     * 系统消息日志
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function messageLog()
    {
        $arr_search = Request::input();
        $res = LogService::get_message_list( (array) $arr_search );
        //逻辑还没有处理完
        echo_error($res);
        return view('admin::index.message_log', $res['data'] );
    }


    /**
     *  极光推送消息列表
     *  hinq 2020 5 12
     *
     **/
    public function jpush_list_log(){
        $res = LogService::get_jpush_list();
        echo_error($res);
        return view('admin::index.jpush_log', $res['data'] );
    }



    /**
     * 富文本上传
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function rich_text_upload(){
        try {
            $arr_input = Request::input();
            $n_id = intval(Request::input('id',0));
            if($n_id <= 0){
                $res = DB::table('atest')->insert($arr_input);
            }
            else {
                $res = DB::table('atest')->where(['id'=>$n_id])->update($arr_input);
            }
            if(!$res){
                return hello_error('失败1');
            }
            else{
                return  hello_success('成功');
            }

        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

    /**
     * 获取富文本
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function rich_text(){
        try {
            $n_id = intval(Request::input('id',0));
            $res = DB::table('atest')->find($n_id);
            if(is_null($res)){
                $res = [];
            }
            else {
                $res = [
                  'id'=>$res->id,
                  'content'=>$res->content,
                ];
            }
            return view('admin::index.rich_text', $res );

        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

    public function file_upload(){
        return view('admin::index.upload_file');
    }

    public function element_upload_file(){
        $arr_get = Request::input();
        $arr_get['file_dir'] = 'ele_upload';
        $res = upload_one_file($_FILES["file"],$arr_get);
        return $res;
    }

    public function init_table(){
        $arr_data = [
          ['id'=>1,'username' => 'admin', 'password' => Hash::make('admin123')],
          ['id'=>2,'username' => 'wu', 'password' => Hash::make('wu555662')]
        ];
        DB::table('admins')
          ->insert($arr_data);

        // Create table for storing roles
        $arr_data = [
          ['id'=>1,'name' => '超级管理员', 'display_name' => '超级管理员', 'description' => '超级管理员'],
          ['id'=>2,'name' => '管理员', 'display_name' => '管理员', 'description' => '管理员']
        ];
        DB::table('entrust_roles')
          ->insert($arr_data);
        
        $arrTopData = [
          ['id' => 1, 'route' => '#系统设置', 'alias' => '#系统设置', 'name' => '系统设置', 'pid' => 0, 'sort' => 1, 'is_menu' => 1],
          ['id' => 2, 'route' => '#日志管理', 'alias' => '#日志管理', 'name' => '日志管理', 'pid' => 0, 'sort' => 2, 'is_menu' => 1],
        ];
        DB::table('entrust_permissions')
          ->insert($arrTopData);//添加顶级权限

        $arrSonData = [
            //          ['route' => 'admin-index-main', 'alias' => 'admin_index_main', 'name' => '首页', 'pid' => 1, 'sort' => 1, 'is_menu' => 1],
            ['route' => 'admin-permission-index', 'alias' => 'admin_permission_index', 'name' => '权限列表', 'pid' => 1, 'sort' => 2, 'is_menu' => 1],
            ['route' => 'admin-role-index', 'alias' => 'admin_role_index', 'name' => '角色列表', 'pid' => 1, 'sort' => 3, 'is_menu' => 1],
            ['route' => 'admin-admin-index', 'alias' => 'admin_admin_index', 'name' => '管理员列表', 'pid' => 1, 'sort' => 4, 'is_menu' => 1],
            ['route' => 'admin-index-clear-cache', 'alias' => 'admin_index_clear_cache', 'name' => '清除缓存', 'pid' => 1, 'sort' => 5, 'is_menu' => 1],
            ['route' => 'admin-log-login-log', 'alias' => 'admin_log_login_log', 'name' => '登录日志', 'pid' => 2, 'sort' => 1, 'is_menu' => 1],
            ['route' => 'admin-log-action-log', 'alias' => 'admin_log_action_log', 'name' => '操作日志', 'pid' => 2, 'sort' => 2, 'is_menu' => 1],
            ['route' => 'admin-log-sms-log', 'alias' => 'admin_log_sms_log', 'name' => '短信日志', 'pid' => 2, 'sort' => 3, 'is_menu' => 1],
        ];
        DB::table('entrust_permissions')
          ->insert($arrSonData);//添加二级权限

        // Create table for associating permissions to roles (Many-to-Many)
        DB::table('entrust_permission_role')
          ->insert([['permission_id' => 1, 'role_id' => 2], ['permission_id' => 1, 'role_id' => 1]]);

        // Create table for associating roles to users (Many-to-Many)
        DB::table('entrust_role_user')
          ->insert(['user_id' => 1, 'role_id' => 1]);
        DB::table('entrust_role_user')
          ->insert(['user_id' => 2, 'role_id' => 2]);
    }


    public function tips(){

        $arr_data = [];

        /*用户账号*/
        //管理后台
        $arr_option = [
          'field'=>['id','username','pass','phone'],
          'where'=>['status'=>1],
        ];
        $res = AdminsRepository::base_get_all($arr_option)->toarray();
        $arr_data['user_admin'] = $res;

        //商户后台
        $arr_option = [
          'field'=>['id','username','phone','pass'],
          'where'=>['is_master'=>1,'state'=>1],
        ];
        $res = ShopUserRepository::base_get_all($arr_option)->toarray();
        $arr_data['user_shop'] = $res;


        //用户
        $arr_option = [
          'field'=>['id','username','phone','pass','nickname'],
          'where'=>['status'=>1],
        ];
        $res = UsersRepository::base_get_all($arr_option)->toarray();
        $arr_data['user'] = $res;

        //微信小程序-今晨代驾
        $arr_data['jc_mini'] = ['username'=>'1769847797@qq.com','pass'=>'lhy123456'];

        //微信小程序-信达养车
        $arr_data['xd_mini'] = ['username'=>'3111260835@qq.com','pass'=>'lhy123456','AppID'=>'wx4848cf1b4f659a5c','AppSecret'=>'07641f1bb9591e5bd30b985c7b573f4d'];

        //微信公众号-信达养车
        $arr_data['xd_wechat'] = ['username'=>'2511234566@qq.com','pass'=>'1123-yhg','AppID'=>'wxe32410ce2999a155','AppSecret'=>'e1c954e39a33b292a2c29c500fa394ba'];

        //微信支付商户号-信达养车
        $arr_data['xd_wechat_payment'] = ['username'=>'1553306381'];

        //阿里云账号-信达养车
        $arr_data['xd_ali_account'] = ['username'=>'15287939353/云南爱车坊科技','pass'=>'lhy123456'];

        //融云-信达养车
        $arr_data['xd_rongyun_account'] = ['url'=>'https://developer.rongcloud.cn/signup','username'=>'15287939353','pass'=>'lhy123456'];

        //极速数据-信达养车
        $arr_data['xd_jisu_account'] = ['url'=>'https://www.jisuapi.com/my/reg','username'=>'15287939353','pass'=>'lhy123456'];

        //高德地图-信达养车
        $arr_data['xd_gaode_account'] = ['url'=>'http://lbs.amap.com/','username'=>'15188111123','pass'=>'1123yhg'];

        //测试项目域名-信达养车后台
        $arr_data['xd_test'] = [
          'admin_http'=>'http://test-jcadmin.7deer.cn',
          'shop_http'=>'http://test-jcshop.7deer.cn',
          'api_http'=>'http://test-jcapi.7deer.cn',
          'web_http'=>'http://test-jcweb.7deer.cn',
          'mini_http'=>'http://test-jcmini.7deer.cn',
          'wechat_http'=>'http://test-jcwechat.7deer.cn',
        ];

        //正式项目域名-信达养车后台
        $arr_data['xd_offical'] = [
          'admin_http'=>'http://jin_admin.7deer.cn',
          'shop_http'=>'http://jin_shop.7deer.cn',
          'api_http'=>'http://jin-api.7deer.cn',
          'web_http'=>'http://jin_web.7deer.cn',
          'mini_http'=>'https://xdminiapi.chxing.cn',
          'wechat_http'=>'http://jinwechat.7deer.cn',
        ];
//        dd($arr_data);

        return view('admin::index.tips',$arr_data);
    }






}
